﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Xunit;
using Genuilder;
using System.IO;
using Genuilder.Extensibility;

namespace Genuilder.Tests
{
	public class TestClass
	{
		const string ProjectList = "../../Data/ProjectList";
		const string ProjectFile = "../../Data/NMigrate.csproj";
		const string TargetFile = "../../Data/AppSettingsGenerator.targets";
		const string ExtensibilityTargetFile = "Genuilder.Extensibility.targets";
		[Fact]
		public void CanListProjectInDirectories()
		{
			Assert.Equal(4, Projects.InSubDirectories(ProjectList).Count());
		}

		[Fact]
		public void CanFilterProjectByAssembly()
		{
			Assert.Equal(3, Projects.InSubDirectories(ProjectList).ExceptForAssembly("NMigrateThis").Count());
		}

		[Fact]
		public void CanInstallFeature()
		{
			using(var file = new TestFile().Of(ProjectFile))
			{
				var project = new Project(file.FileInfo.FullName);
				project.InstallFeature(new TargetFeature(TargetFile));
				project.Save();
				project.MsBuildData.ProjectRootElement.Imports.AssertHas(TargetFile);
			}
		}
		[Fact]
		public void CanUninstallFeatures()
		{
			using(var file = new TestFile().Of(ProjectFile))
			{
				var project = new Project(file.FileInfo.FullName);
				project.InstallFeature(new TargetFeature(TargetFile));
				project.Save();
				project.UnInstallFeatures();
				project.MsBuildData.ProjectRootElement.Imports.AssertHasNot(TargetFile);
			}
		}

		[Fact]
		public void CanInstallExtensions()
		{
			using(var file = new TestFile().Of(ProjectFile))
			{
				var project = new Project(file.FileInfo.FullName);
				var extensibility = new ExtensibilityFeature(ExtensibilityTargetFile);
				var extension = extensibility.AddExtension(new CreateFileExtension());
				project.InstallFeature(extensibility);
				project.Save();
				Assert.False(File.Exists(extension.File));
				Assert.True(project.Build().Success);
				Assert.True(File.Exists(extension.File));
			}
		}


		[Fact]
		public void CanInstallCommonFeature()
		{
			using(var file = new TestFile().Of(ProjectFile))
			{
				var project = new Project(file.FileInfo.FullName);
				project.CommonFeaturesPath = "../../Data";
				project.InstallCommonFeature(CommonFeature.AppSettingsGenerator);
				project.Save();
			}
		}
	}
}
